
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mathias Olsson
 */
public class Panel extends JPanel implements ActionListener, ListSelectionListener {

    private JPanel topPanel;
    private JTextArea travels = new JTextArea();;
    private JTextArea travelInfo;
    private JPanel midPanel;
    private JPanel botPanel;
    private JScrollPane scrollPane;
    private JLabel search;
    private JComboBox destination;
    private JList selectedDestinationTravels;
    private Vector destinationTravels; // We need a vector to keep track of the actual flights
    // so that we can fill a textarea with them.
    String dest[] = {"Norway", "Denmark", "Finland"};
    // String book = ("Book");

    public Panel() {
        super(new BorderLayout());
        this.setPreferredSize(new Dimension(600, 600));

        destinationTravels = null;
        init();

    }

    public void init() {

        //Buttons
        JButton bookButton = new JButton("Book");
        bookButton.setPreferredSize(new Dimension(80, 100));
        bookButton.addActionListener(this);
        // Panels
        topPanel = new JPanel();
        midPanel = new JPanel();
        botPanel = new JPanel();

        midPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));
        midPanel.setPreferredSize(new Dimension(370, 200));

        //Combobox
        destination = new JComboBox();
        destination.addActionListener(this);
        fillDestinationBox();
        destination.setSelectedIndex(0);

        //TextArea
       
        travels.setPreferredSize(new Dimension(300, 100));
        travels.setEditable(false);

        //Labels
        search = new JLabel("Select a location to view available travels.");

        //JList
        selectedDestinationTravels = new JList();
        selectedDestinationTravels.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        selectedDestinationTravels.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        selectedDestinationTravels.addListSelectionListener(this);
        selectedDestinationTravels.setVisible(true);
        JScrollPane listScrollPane = new JScrollPane(selectedDestinationTravels);
        listScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        selectedDestinationTravels.setSize(new Dimension(370, 200));
        selectedDestinationTravels.setFixedCellWidth(300);

        // Adding components
        topPanel.add(search, BorderLayout.WEST);
        topPanel.add(destination, BorderLayout.EAST);

        midPanel.add(selectedDestinationTravels, BorderLayout.NORTH);
        selectedDestinationTravels.add(listScrollPane);

        botPanel.add(travels);
        botPanel.add(bookButton);

        this.add(topPanel, BorderLayout.NORTH);
        this.add(midPanel, BorderLayout.CENTER);
        this.add(botPanel, BorderLayout.SOUTH);


        selectedDestinationTravels.setListData(dest);

    }

    /*
     * Method for filling the JComboBox "destination"
     * Not yet DbQueries-implemented
     */
    private void fillDestinationBox() {
        for (int i = 0; i < dest.length; i++) {
            destination.addItem(dest[i]);
        }
    }

    /*
     * Compares chosen object with db, to see if there are anything to add to the JList
     */
    private void addVectorToJList() {
        destinationTravels = new Vector();
        /*
         * !!!Pseudo!!!
         * if (valt resemål har några tillgängliga resor i databasen) {             Frågan är hur man lägger till ngt i en
         *          for( int i = 0; i < mängden tillgängliga resor; i++) {              vektor utan att objektet är vektor...
         *              destinationTravels.add( vad vi ska lägga till )                     kanske går att lösa?
         *          }
         *      }
         *      selectedDestinationTravels.setListData(destinationTravels);     Lägg till vectorn i vår JList
         *      selectedDestinationTravels.setSelectedIndex(0);                         Välj den översta
         */
    }


    /*
     * Get Methid for the JPanel "botPanel"
     * Currently unused.
     */
    public JPanel getBotPanel() {
        return midPanel;
    }

    /*
     * Not yet implemented
     */
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals("Book")) {
            System.out.println("Välkommen till Afrika!");

        } else {
            JComboBox cb = (JComboBox) e.getSource();
            String newSelect = (String) cb.getSelectedItem();
            Handler h = new Handler();
            String destination = h.searchDestination(newSelect);
            if(destination!=null)
            travels.setText(destination);
            
            
            /*
             * We now have a string (newSelect) that always holds the selected object in the combobox.
             * We will use this to update the textareas when we get that far.
             */
        }
    }

    /*
     * Not yet implemented
     */
    public void valueChanged(ListSelectionEvent e) {
    }
}
